<template>
  <div>
    <el-container class="home-container">
      <!-- 头部区 -->
      <el-header>
        <div>
          <img
            src="../assets/树.png"
            alt=""
            style="width: 80px; height: 60px"
          />
          <span>小胡杨</span>
        </div>
        <el-button type="info" @click="logout">退出登录</el-button></el-header
      >
      <!-- 页面主体区 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse" >|||</div>
          <!-- 侧边栏菜单区 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
          >
            <!-- 一级菜单 -->
            <el-submenu v-for="item in menulist" :key="item.id" :index="item.id+''">
              <!-- 一级菜单模板区-->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-location"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>

            <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path+''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path+'')">
                <!-- 二级菜单模板区-->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容展示区 -->
        <el-main>
          <!-- 路由占位符 ,欢迎界面-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单栏数据
      menulist:[],
      // 存放菜单栏的头像图标,动态绑定样式原理
      iconObj:{},
      // 控制菜单栏是否折叠
      isCollapse:false,
      // 被激活的链接地址
      activePath:''
    }
  },
  // 定义钩子函数，在页面加载时发起请求，获取菜单栏信息
  created(){
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录方法，清空token（核心），强制跳转到登录页
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    // 获取菜单栏信息的方法
    async getMenuList(){
      // 用async和await简化promise结果，将后台返回的数据解构并将最有用的data数据提取出来赋值给res
    const {data:res}= await this.$http.get('menus');
      // 如果获取菜单数据失败，返回错误信息
    if(res.meta.status!==200) return this.$message.error(res.mate.msg);
      // 获取菜单栏信息成功，将数据赋值给页面要读取的数组
    this.menulist=res.data;
    },

    // 点击按钮，实现菜单的展开与折叠
    toggleCollapse(){
      this.isCollapse=!this.isCollapse;
    },

    // 保持链接的激活状态
    saveNavState(activePath){
       window.sessionStorage.setItem('activePath',activePath);
       this.activePath=activePath;
    }
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>